
function getSensorsState()
{
	var req = false;

	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var sensorsValue = eval( "(" + req.responseText + ")" );
				var maxInd = window.frames.length - 1;
				for (var i=1; i<= maxInd; i++)
				{
					var labelState=0;
					if ((document.getElementById("id_" + i+ "") != null) && (document.getElementById("id_" + i+ "").contentDocument != null) && 
						(document.getElementById("id_" + i+ "").contentDocument.getElementById("label_state") != null))
					{
						labelState = document.getElementById("id_" + i+ "").contentDocument.getElementById("label_state");
						labelState.innerHTML = "<div>" + sensorsValue[i+''] + "</div>";
					}
				}
			}
			else if (req.status == 500)		//server fault!!!
			{
				document.location = './index.htm';
			}
		}
	}

	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (req)
	{
		req.open("GET", "/sensors?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(null);
	}
}

var formVersionCur=-1;
var formDataOld = [];

function setFormValues(ind, doc, formVal, formType)
{	
//ind: 1, 2, 3...
	var pageType = 2;	
	for(var i = 0; i < doc.iocontrol.elements.length; i++) 
		if (doc.iocontrol.elements[i].name == 'btn_start')
		{
			pageType = 1;
			break;
		}

	for(var i = 0; i < doc.iocontrol.elements.length; i++) 
		if ((doc.iocontrol.elements[i].name != 'btn_close') && (doc.iocontrol.elements[i].name != 'btn_start') && (doc.iocontrol.elements[i].name != 'btn_print'))  // && (doc.iocontrol.elements[i].name != 'answer_code'))
		{
			if ((formType == 2) && (pageType == 2))
			{
				doc.iocontrol.elements[i].disabled = true;
				//if (formVal == '')
				//	doc.iocontrol.elements[i].value = '';
			}
			else
				doc.iocontrol.elements[i].disabled = false;
		}
				
	if (formVal != '')		//lock forms:)
	{

		var arr = formVal.split('&');
		//alert(formVal + ' - '+ arr.length);

		for (var i=0; i < arr.length; i++)
		{
			var ctrlArr = arr[i].split('=');
			if (ctrlArr[0] == 'sensor_id')
				continue;
			//alert(ctrlArr[0] + ' '+ ctrlArr[1]);


			for(var i = 0; i < doc.iocontrol.elements.length; i++) 
				if (doc.iocontrol.elements[i].name == ctrlArr[0])
				{
					doc.iocontrol.elements[i].value = ctrlArr[1];
					break;
				}
		}
	}
	else if (formDataOld[ind] != formVal)
	{
		//alert('clear form! index:' + ind);
		for(var i = 0; i < doc.iocontrol.elements.length; i++) 
		{
			if (doc.iocontrol.elements[i].type == 'text')
				doc.iocontrol.elements[i].value = '';
			if (doc.iocontrol.elements[i].type == 'select-one')
				doc.iocontrol.elements[i].selectedIndex = 0;			
		}
	}

	formDataOld[ind] = formVal;
}

function getFormsState()
{
	var req = false;

	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var formValues = eval( "(" + req.responseText + ")" );
				var formValuesLen=0, formVersion=0, formType=0;
				var formData = [];
				for (var i=0; i < 8; i++)
					formData[i] = '';
				for (var q in formValues) 
				{
					if (q == 'version')
						formVersion = (formValues[q]) - 0;
					else if (q == 'forms_type')
						formType = (formValues[q]) - 0;
					else
						formData[q-1] = (formValues[q]);
					formValuesLen++;
				}
				if (formVersion == formVersionCur)
					return;

				//alert(formVersionCur + ' -> ' + formVersion);
				formVersionCur = formVersion;

				var maxInd = window.frames.length - 1;
				for (var i=1; i<= maxInd; i++)
				{
					//var cardType=0;
					if ((document.getElementById("id_" + i+ "") != null) && (document.getElementById("id_" + i+ "").contentDocument != null) && 
						(document.getElementById("id_" + i+ "").contentDocument.getElementById("id_card_type") != null))
					{
						setFormValues(i, document.getElementById("id_" + i+ "").contentDocument, formData[i-1], formType);
					}
				}
			}
		}
	}

	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (req)
	{
		req.open("GET", "/forms?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(null);
	}
}

function getSelectOptions()
{
	getSelectOptionsCard();
	getSelectOptionsLevel();
	getSelectOptionsEvent();
}

function getSelectOptionsCard()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var optionValues = eval( "(" + req.responseText + ")" );
				var optionValuesLen=0;
				for (var q in optionValues) optionValuesLen++;
				if (optionValuesLen == 0)
				{	//no data jet
					setTimeout(getSelectOptionsCard, 10000);	//10 sec
					return;
				}
				var maxInd = window.frames.length - 1;
				for (var i=1; i<= maxInd; i++)
				{
					var cardType=0;
					if ((document.getElementById("id_" + i+ "") != null) && (document.getElementById("id_" + i+ "").contentDocument != null) && 
						(document.getElementById("id_" + i+ "").contentDocument.getElementById("id_card_type") != null))
					{
						cardType = document.getElementById("id_" + i+ "").contentDocument.getElementById("id_card_type");
						cardType.disabled = false;
						var num=1;	
				   		for (var key in optionValues)
			 	       			cardType.options[num++] = new Option(optionValues[key], key, false, false);
					}
				}
			}
			else
				setTimeout(getSelectOptionsCard, 10000);	//10 sec
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		req.open("GET", "/dbvalues_card?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(null);
	}
}

function getSelectOptionsLevel()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var optionValues = eval( "(" + req.responseText + ")" );
				var optionValuesLen=0;
				for (var q in optionValues) optionValuesLen++;
				if (optionValuesLen == 0)
				{	//no data jet
					setTimeout(getSelectOptionsLevel, 10000);	//10 sec
					return;
				}
				var maxInd = window.frames.length - 1;
				for (var i=1; i<= maxInd; i++)
				{
					var cardType=0;
					if ((document.getElementById("id_" + i+ "") != null) && (document.getElementById("id_" + i+ "").contentDocument != null) && 
						(document.getElementById("id_" + i+ "").contentDocument.getElementById("id_level_type") != null))
					{
						cardType = document.getElementById("id_" + i+ "").contentDocument.getElementById("id_level_type");
						cardType.disabled = false;
						var num=1;	
				   		for (var key in optionValues)
			 	       			cardType.options[num++] = new Option(optionValues[key], key, false, false);
					}
				}
			}
			else
				setTimeout(getSelectOptionsLevel, 10000);	//10 sec
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		req.open("GET", "/dbvalues_level?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(null);
	}
}

function getSelectOptionsEvent()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var optionValues = eval( "(" + req.responseText + ")" );
				var optionValuesLen=0;
				for (var q in optionValues) optionValuesLen++;
				if (optionValuesLen == 0)
				{	//no data jet
					setTimeout(getSelectOptionsEvent, 10000);	//10 sec
					return;
				}
				var maxInd = window.frames.length - 1;
				for (var i=1; i<= maxInd; i++)
				{
					var cardType=0;
					if ((document.getElementById("id_" + i+ "") != null) && (document.getElementById("id_" + i+ "").contentDocument != null) && 
						(document.getElementById("id_" + i+ "").contentDocument.getElementById("id_event_type") != null))
					{
						cardType = document.getElementById("id_" + i+ "").contentDocument.getElementById("id_event_type");
						cardType.disabled = false;
						var num=1;	
				   		for (var key in optionValues)
			 	       			cardType.options[num++] = new Option(optionValues[key], key, false, false);
					}
				}
			}
			else
				setTimeout(getSelectOptionsEvent, 10000);	//10 sec
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		req.open("GET", "/dbvalues_event?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(null);
	}
}

function setFormDisable(disablVal, type)	//type: end -> 1, start -> 2
{
	//document.iocontrol.answer_code.disabled = disablVal;
	if (type == 1)
		document.iocontrol.btn_close.disabled = disablVal;
	else
		document.iocontrol.btn_start.disabled = disablVal;
}

function getRequestBody(oForm) 
{
	var aParams = new Array();
   for(var i = 0; i < oForm.elements.length; i++) 
	   if ((oForm.elements[i].name != "btn_start") && (oForm.elements[i].name != "btn_close") && (oForm.elements[i].name != "btn_print") && (oForm.elements[i].name != "answer_code"))
	   {
	      var sParam = encodeURIComponent(oForm.elements[i].name);
			sParam += "=";
	      sParam += encodeURIComponent(oForm.elements[i].value);
        	aParams.push(sParam);
		}
	return aParams.join("&");
}     

function doStart()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				setFormDisable(false, 2);

				document.iocontrol.answer_code.value = req.responseText;
			}
			//else
			//	setTimeout(doStart, 5000);	//5 sec
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		setFormDisable(true, 2);

		req.open("POST", "/action_start?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(getRequestBody(document.iocontrol));
	}
}


function doEnd()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				setFormDisable(false, 1);

				document.iocontrol.answer_code.value = req.responseText;
			}
			//else
			//	setTimeout(doEnd, 5000);	//5 sec
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		setFormDisable(true, 1);

		req.open("POST", "/action_end?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(getRequestBody(document.iocontrol));
	}
}

function userAdd()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			document.form_users.btn_user_add.disabled = false;
			if (req.status == 200)
			{
				alert(req.responseText);
			}
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		document.form_users.btn_user_add.disabled = true;

		req.open("POST", "/user_add?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(getRequestBody(document.form_users));
	}
}

function userChange()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			document.form_pass.btn_user_pass.disabled = false;
			if (req.status == 200)
			{
				alert(req.responseText);
			}
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		document.form_pass.btn_user_pass.disabled = true;

		req.open("POST", "/user_change?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send(getRequestBody(document.form_pass));
	}
}

function doPrintSensor()
{
	var req = false;
	function reqComplete()
	{
		if (req.readyState == 4)
		{
			document.iocontrol.btn_print.disabled = false;
			if (req.status == 200)
			{
				document.getElementById("id_print_user").innerHTML = req.responseText;
				window.print();
			}
		}
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");

	if (req)
	{
		document.iocontrol.btn_print.disabled = true;

		time=new Date();
		time_sec=time.getSeconds();
		time_min=time.getMinutes();
		time_hours=time.getHours();
		time_wr=((time_hours<10)?"0":"")+time_hours;
		time_wr+="-";
		time_wr+=((time_min<10)?"0":"")+time_min;
		time_wr+="-";
		time_wr+=((time_sec<10)?"0":"")+time_sec;
		date_wr = time.getDate()+"."+ ((time.getMonth() < 10) ? '0' : '') +time.getMonth()+"."+time.getFullYear();
		obj = document.getElementById("id_print_time");
		obj.innerHTML=time_wr;
		obj = document.getElementById("id_print_date");
		obj.innerHTML=date_wr;

		document.getElementById("id_print_card_type").innerHTML =  document.iocontrol.card_type.options[document.iocontrol.card_type.selectedIndex].text;
		document.getElementById("id_print_level_type").innerHTML =  document.iocontrol.level_type.options[document.iocontrol.level_type.selectedIndex].text;
		document.getElementById("id_print_event_type").innerHTML =  document.iocontrol.event_type.options[document.iocontrol.event_type.selectedIndex].text;

		document.getElementById("id_print_card").innerHTML =  document.iocontrol.card.value;
		document.getElementById("id_print_card_number").innerHTML =  document.iocontrol.card_number.value;
		document.getElementById("id_print_event_descr").innerHTML =  document.iocontrol.event_descr.value;
		document.getElementById("id_print_event_code").innerHTML =  document.iocontrol.event_code.value;
		document.getElementById("id_print_answer_code").innerHTML =  document.iocontrol.answer_code.value;

		req.open("POST", "/action_print?id=" + Math.random(), true);
		req.onreadystatechange = reqComplete;
		req.send("sensor_id=" + document.iocontrol.sensor_id.value + "&");
	}
}


